package com.zzyl.serve.controller;

import java.util.List;
import java.util.Map;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备状态Controller
 * 
 * @author huangqin
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备状态相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备状态列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备状态列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

/**
 * 1.1 从物联网平台同步产品列表
 * 接口地址:/nursing/device/syncProductList
 * 请求方式:POST
 */
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return AjaxResult.success();
}

/**
 * 1.2 查询所有产品列表
 * 接口地址:/nursing/device/allProduct
 * 请求方式:GET
 */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public AjaxResult allProduct() {
       List<ProductVo> productVos = deviceService.allProduct();
       return AjaxResult.success(productVos);
    }

/**
 * 1.3 注册设备
 * 接口地址:/nursing/device/register
 * 请求方式:POST
 */
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return AjaxResult.success();
    }
    
    /**
     * 1.5 查询设备详细数据
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:GET
     */
    @GetMapping("/{iotId}")
    @ApiOperation("查询设备详细数据")
    public AjaxResult getDeviceDetail(@PathVariable("iotId") String iotId) {
       DeviceDetailVo deviceDetailVo = deviceService.getDeviceDetail(iotId);
       return AjaxResult.success(deviceDetailVo);
    }

    /**
     * 1.6 查看设备上报的数据
     * 接口地址: /nursing/device/queryServiceProperties/{iotId}
     * 请求方式:GET
    */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查看设备上报的数据")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
      List<Map<String, Object>> list = deviceService.queryServiceProperties(iotId);
        return AjaxResult.success(list);
    }

    /**
     * 1.7 修改设备
     * 接口地址:/nursing/device
     * 请求方式:PUT
     */
    @PutMapping()
    @ApiOperation("修改设备")
    public AjaxResult updateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDeviceDto(deviceDto);
        return AjaxResult.success();
    }

    /**
     * 1.8 删除设备
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:DELETE
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult deleteDevice(@PathVariable("iotId") String iotId) {
        deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }

    /**
     * 1.9 查询产品详情(报警规则模块使用)
     * 接口地址:/nursing/device/queryProduct/{productKey}
     * 请求方式:GET
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey) {
      return  deviceService.queryProduct(productKey);
    }
}
